#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2019-06-24 10:08
# @Author   : BYF
# file      : config.py
# @version  :

# import const
import os
import configparser
import datetime


class Config():
    def __init__(self):
        """
        初始化
        """
        # 基础文件目录
        self.cur_path = os.path.abspath(__file__)
        self.project_dir = os.path.dirname(os.path.dirname(self.cur_path))
        self.conf_path = os.path.join(self.project_dir, "conf/config.ini")

        self.config = configparser.ConfigParser()
        if not os.path.exists(self.conf_path):  # 判断文件是否存在
            print("配置文件未找到~！")
        self.config.read(self.conf_path)

        self.strtime = datetime.datetime.now().strftime("%Y-%m-%d %X")

        # 日志配置
        self.logger_name = self.get_conf("log", "LOGGER_NAME")
        self.log_level = self.get_conf("log", "LOG_LEVEL")
        self.log_conf = os.path.join(self.project_dir, self.get_conf("log", "LOG_CONF_PATH"))
        self.log_dir = os.path.join(self.project_dir, self.get_conf("log", "LOG_FILE_PATH"))

        # 用例配置
        self.case_data_path = os.path.join(self.project_dir, self.get_conf("case", "CASE_DATA_PATH"))
        self.case_run_path = os.path.join(self.project_dir, self.get_conf("case", "CASE_RUN_PATH"))

        # 报告配置
        self.report_xml_dir = os.path.join(self.project_dir, self.get_conf("report", "REPORT_XML_PATH"))
        self.report_html_dir = os.path.join(self.project_dir, self.get_conf("report", "REPORT_HTML_PATH"))
        self.report_xml_zip_dir = os.path.join(self.project_dir, self.get_conf("report", "REPORT_XML_ZIP_PATH"))
        self.report_html_zip_dir = os.path.join(self.project_dir, self.get_conf("report", "REPORT_HTML_ZIP_PATH"))
        self.report_zip_to_path = os.path.join(self.project_dir, self.get_conf("report", "REPORT_ZIP_TO_PATH"))

        # 环境配置
        self.private_debug = self.get_conf("domain", "PRIVATE_DEBUG")
        self.online_release = self.get_conf("domain", "ONLINE_RELEASE")

        # 邮件配置
        self.smtp_server = self.get_conf("mail", "SMTP_SERVER")
        self.smtp_port = self.get_conf("mail", "SMTP_PORT")
        self.smtp_user = self.get_conf("mail", "SMTP_USER")
        self.smtp_password = self.get_conf("mail", "SMTP_PASSWORD")
        self.smtp_sender = self.get_conf("mail", "SMTP_SENDER")
        self.smtp_receivers = self.get_conf("mail", "SMTP_RECEIVERS")
        self.smtp_cc = self.get_conf("mail", "SMTP_CC")
        self.smtp_send_email = self.get_conf("mail", "SMTP_SEND_EMAIL")  # 是否发送邮件

        # 数据库配置
        self.db_host = self.get_conf("db", "DB_HOST")
        self.db_port = self.get_conf("db", "DB_PORT")
        self.db_user = self.get_conf("db", "DB_USER")
        self.db_password = self.get_conf("db", "DB_PASSWORD")
        self.db_name = self.get_conf("db", "DB_NAME")

        # 钉钉配置
        self.robot_secret_tester = self.get_conf("dingding", "ROBOT_SECRET_TESTER") # 群：测试大哥们中的小官机器人

    def get_conf(self, section, option):
        """
        获取配置文件
        :param section:
        :param option:
        :return:
        """
        value = self.config.get(section, option)
        return value

    def set_conf(self, section, option, value):
        """
        修改配置文件
        :param section:
        :param option:
        :param content:
        :return:
        """
        self.config.set(section, option, value)
        with open(self.conf_path, "w+") as file:
            self.config.write(file)

    def add_conf(self, section):
        """
        添加配置文件
        :param section:
        :return:
        """
        self.config.add_section(section)
        with open(self.conf_path, "w+") as file:
            self.config.write(file)


if __name__ == '__main__':
    config = Config()
